﻿
Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_DangKy_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

        End If
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim p As New VSModel.DangKy
            Try

                p.HoTen = txtHoten.Text.Trim()
                p.DiaChi = txtDiachi.Text.Trim()
                p.TenCongTy = txtTencongty.Text.Trim()
                p.Email = txtEmail.Text.Trim()
                p.DienThoai = txtDienthoai.Text.Trim()
                p.LoaiMau = txtLoaimau.Text.Trim()
                p.MoTaMau = txtMotamau.Text.Trim()
                p.PhuongPhapYeuCau = txtPhuongphapyeucau.Text.Trim()
                p.NgayYeuCau = txtNgayyeucau.Text.Trim()

                p.SoBienNhan = Now.Date.ToString("dd/MM/yyyy")
                data.DangKies.AddObject(p)
                data.SaveChanges()
                p.SoBienNhan = Now.Date.ToString("dd-MM-yyyy") & "-" & p.Id.ToString.PadLeft(3, "0")
                data.SaveChanges()
                'Insert_App_Log("Insert  DangKy:" & txtTitle.Text.Trim & "", Function_Name.DangKy, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                hidID.Value = p.SoBienNhan


                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub ResetControl()

        txtHoten.Text = ""
        txtDiachi.Text = ""
        txtTencongty.Text = ""
        txtEmail.Text = ""
        txtDienthoai.Text = ""
        txtLoaimau.Text = ""
        txtMotamau.Text = ""
        txtPhuongphapyeucau.Text = ""
        txtNgayyeucau.Text = ""

    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            'send email
            'send email thong bao
            Dim templatevars As New Hashtable()
            templatevars.Add("FullName", txtHoten.Text)
            templatevars.Add("BienNhan", hidID.Value)
            Dim parser As New TemplateParser.Parser(Server.MapPath("~/frontend/emailtemplate/BienNhan.htm"), templatevars)

            Sendmail_SMTP("no-reply@hoanvustc.com.vn", "no-reply", "mail.hoanvustc.com.vn", txtEmail.Text, "", "HoanVu dang ky mau thu thanh cong", parser.Parse, "123IGKs1233!@#", 25, False)

            Excute_Javascript("alert('Cập nhật dữ liệu thành công.');window.location ='Page/Thanks.aspx';", Me.Page, True)
        Else
            Excute_Javascript("alert('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnSaveAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAndNew.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');", Me.Page, True)
            ResetControl()
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ResetControl()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
